package com.job.liushuang.HomeWork0719;

import java.util.Stack;

/**
 * @author Liushuang
 * @version 1.0
 * @since 2022-07-19 19:10:40
 */
public class StackSort {
    public static void main(String[] args) {
        int a[]={8,6,3,18};
        sort(a);

    }

    public static void sort(int[] a) {
        Stack<Integer> stack=new Stack<>();

        for (int i = 0; i <a.length ; i++) {
            stack.push(a[i]);
        }
        Stack<Integer> stackassist=new Stack<>();//辅助栈用来比较
        while (!stack.isEmpty()){
            int n=stack.pop();
            if (stackassist.isEmpty()||n<=stackassist.peek()){
                stackassist.push(n);
            }else {
                while (!stackassist.isEmpty()&&n>stackassist.peek()){
                    stack.push(stackassist.pop());
                }
                stackassist.push(n);
            }
        }
        //辅助栈的元素导入到栈中
        //再依次打印出栈
        while (!stackassist.isEmpty()){
            stack.push(stackassist.pop());
        }
        while (!stack.isEmpty()){
            System.out.println(stack.pop());
        }
    }
}
